using Microsoft.AspNetCore.Mvc;
using Common.Helper;
using test.Models;
using Common.Function;
using NuGet.Configuration;

namespace test.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ColumnController : ControllerBase
    {
        private readonly hongjiContext _db;
        public ColumnController(hongjiContext db)
        {
            _db = db;
        }

        [HttpGet(Name = "ColumnID")]
        public Column Get(int ID)
        {
            Column Col = new();
            var ColumnInfo = _db.Columnmanages.Where(x => x.Id == ID).FirstOrDefault();
            if (ColumnInfo != null)
            {
                Col.ID = ColumnInfo.Id;
                Col.ColumnID = ColumnInfo.ColumnId;
                Col.ColumnName = ColumnInfo.ColumnName;
                Col.Description = ColumnInfo.Description;
                Col.Img_url = ColumnInfo.ImgUrl;
                Col.ParentName = CommonFunction.GetColumnName(_db, ColumnInfo.ColumnId);
            }
            return Col;
        }

        [HttpGet("List")]
        public IEnumerable<Column> GetCate(int ColumnID)
        {
            List<Column> Col = new();
            var ColInfo = _db.Columnmanages.Where(x => x.ColumnId == ColumnID).ToList();
            if (ColInfo.Any())
            {
                foreach (var item in ColInfo)
                {
                    Column c = new()
                    {
                        ID = item.Id,
                        ColumnID = item.ColumnId,
                        ColumnName = item.ColumnName,
                        ColumnArray = CommonFunction.GetColumnInfo(_db, item.Id)
                    };
                    Col.Add(c);
                }
            }
            //LogHelper.Write("Invoke Get");
            return Col.ToArray();
        }


        [HttpDelete(Name = "ColumnDelete")]
        public bool Del(int ID = 0)
        {
            List<Column> Col = new();
            hongjiContext db = new();
            var ColumnInfo = db.Columnmanages.FirstOrDefault(x => x.Id == ID);
            if (ColumnInfo != null)
            {
                db.Columnmanages.Remove(ColumnInfo);
            }
            LogHelper.Write("Invoke Delete");
            if (db.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }

        [HttpPost(Name = "ColumnInsert")]
        public bool Post(Column Col)
        {
            hongjiContext db = new();
            var ColInfo = db.Columnmanages.Where(x => x.Id == Col.ID);
            if (ColInfo == null)
            {
                Columnmanage col = new()
                {
                    ColumnId = Col.ColumnID,
                    ColumnName = Col.ColumnName
                };
                db.Columnmanages.Add(col);
            }
            else
            {
                ColInfo.First().ColumnId = Col.ColumnID;
                ColInfo.First().ColumnName = Col.ColumnName;
                db.Columnmanages.Attach(ColInfo.First());
            }

            if (db.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }

    }
}